Murder Rates

Author

Isaac Johnson

Published

November 4, 2024

#Load Library and prep data

Show the code
library(maps)
library(viridis)
library(tidyverse)
library(leaflet)
library(sf)
library(tigris)

# Load the USArrests dataset
data(USArrests)

# Prepare the data for leaflet
arrests_data <- USArrests %>%
  rownames_to_column(var = "state") %>%
  mutate(state = tolower(state))

# Load the US states map data
us_states_map <- map_data("state")

# Define color palette
color_pal <- colorBin(viridis_pal()(5), domain = arrests_data$Murder, bins = 5)

# Join the US states map data with the arrests data
us_states_map_data <- us_states_map %>%
  left_join(arrests_data, by = c("region" = "state"))

# Load the US states shapefile data
us_states_shapefile <- states(class = "sf", resolution = "20m") %>%
  st_transform(4326) %>%
  mutate(state = tolower(NAME))

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
Show the code
# Join the US states shapefile data with the arrests data
us_states_map_data <- us_states_shapefile %>%
  left_join(arrests_data, by = c("state" = "state"))

#Create Leaflet Map

Show the code
# Create the leaflet map
leaflet_map <- leaflet() %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(
    data = us_states_map_data,
    group = "states",
    fillColor = ~color_pal(Murder),
    color = "#BDBDC3",
    fillOpacity = 0.7,
    weight = 1,
    label = ~paste0(NAME, ": ", Murder, " murders per 100,000"),
    labelOptions = labelOptions(direction = "auto")
  ) %>%
  addLegend(
    pal = color_pal,
    values = arrests_data$Murder,
    title = "Murders per 100,000",
    position = "bottomright",
    opacity = 0.7,
    labFormat = labelFormat(prefix = ""),
    labels = c("Low", "Medium-Low", "Medium", "Medium-High", "High")
  )

# Display the leaflet map
leaflet_map